package com.hrms.web.controller.system;

import com.hrms.patient.core.domain.entity.SysUser;
import com.hrms.patient.core.domain.entity.WechatDocter;
import com.hrms.system.service.ISysUserService;
import com.hrms.web.controller.tool.HttpUtilsWechat;
import com.hrms.web.controller.tool.ProduceRegularUser;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.hrms.patient.core.controller.BaseController;
import com.hrms.patient.core.domain.AjaxResult;
import com.hrms.patient.core.domain.model.RegisterBody;
import com.hrms.patient.utils.StringUtils;
import com.hrms.framework.web.service.SysRegisterService;
import com.hrms.system.service.ISysConfigService;

/**
 * 注册验证
 *
 * @author hrms
 */
@RestController
public class SysRegisterController extends BaseController {
    @Autowired
    private SysRegisterService registerService;

    @Autowired
    private ISysConfigService configService;

    @Autowired
    ISysUserService userService;

    @PostMapping("/register")
    public AjaxResult register(@RequestBody RegisterBody user) {
        if (!("true".equals(configService.selectConfigByKey("sys.account.registerUser")))) {
            return error("当前系统没有开启注册功能！");
        }
        String msg = registerService.register(user);
        return StringUtils.isEmpty(msg) ? success() : error(msg);
    }



    /*
     * 微信用户注册
     * */
    @PostMapping("/public/register")
    public AjaxResult Wechatregister(String code, String jobId) {
        HttpUtilsWechat httpUtilsWechat = new HttpUtilsWechat();
        String openId = httpUtilsWechat.getOpenid(code);
/*        openId = "123344";
        jobId = "111";*/
        ProduceRegularUser produceRegularUser = new ProduceRegularUser();
        if(jobId != null && jobId !="")
        {
            //此时是医生在注册
            //根据jobId更新医生的openId
            WechatDocter docter = new WechatDocter();
            docter = userService.selectDocterByJobId(jobId);
            if(docter==null)return AjaxResult.error("工号有误");
            docter.setDocOpenId(openId);
            docter.setJobId(jobId);
            if(userService.updateDocOpenId(docter)>0){
                SysUser user = produceRegularUser.ProduceDocter(openId,docter);
                if(userService.selectUserByopenId(openId)!=null) {
                    return AjaxResult.error("用户已存在!");
                }
                else{
                    userService.insertUser(user);
                    return AjaxResult.success("注册成功！");
                }
            }
            else{
                return AjaxResult.error("工号有误！");
            }
        }


        else {
            //此时是病人在注册
            if (openId != null) {
                SysUser user = produceRegularUser.ProduceUser(openId);
                if(userService.selectUserByopenId(openId)!=null) {
                    return AjaxResult.error("用户已存在!");
                }
                else{
                    return toAjax(userService.insertUser(user));
                }
            } else {
                return AjaxResult.error("error", null);
            }
        }
    }




}
